package com.samebuilding.web.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.samebuilding.domain.Comment;
import com.samebuilding.domain.CommentReply;
import com.samebuilding.domain.User;
import com.samebuilding.service.CommentService;
import com.samebuilding.util.HtmlUtil;
import com.samebuilding.util.PaginationUtil;

public class CommentAction extends DispatchAction {

	public static final int BLOG = 1;
	public static final int PHOTO = 2;
	public static final int ACTIVITY = 3;
	
	private CommentService commentService;
	
	
	public CommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	public ActionForward allReceive(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 10;
		//System.out.println("1111");
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = Integer.valueOf(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getIReceiveComment(uid, start, limit);
		
		int count = this.commentService.getCountIReceiveComment(uid);
		
		
		int unread = this.commentService.getCountUnreadReceiveComment(uid);
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		request.setAttribute("unread", unread);
		
		
		
		return mapping.findForward("allReceive");
	}
	
	public ActionForward allSend(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 10;
		//System.out.println("1111");
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = Integer.valueOf(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getISendComment(uid, start, limit);
		
		int count = this.commentService.getCountISendComment(uid);
		
		
		int unread = this.commentService.getCountUnreadCommentReply(uid);
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		
		
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		request.setAttribute("unread", unread);
		
		
		
		return mapping.findForward("allSend");
	}

	public ActionForward blogSend(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 10;
		//System.out.println("1111");
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = Integer.valueOf(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getISendCommentByType(uid, BLOG, start, limit);
		
		int count = this.commentService.getCountISendCommentByType(uid, BLOG);
		
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		
		
		//System.out.println(url);
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		
		
		
		return mapping.findForward("blogSend");
	}

	public ActionForward blogReceive(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 10;
		//System.out.println("1111");
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = Integer.valueOf(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getIReceiveCommentByType(uid, BLOG, start, limit);
		
		int count = this.commentService.getCountIReceiveCommentByType(uid, BLOG);
		
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		
		
		//System.out.println(url);
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		
		
		
		return mapping.findForward("blogReceive");
	}
	
	public ActionForward photoSend(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 10;
		//System.out.println("1111");
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = Integer.valueOf(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getISendCommentByType(uid, PHOTO, start, limit);
		
		int count = this.commentService.getCountISendCommentByType(uid, PHOTO);
		
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		
		
		//System.out.println(url);
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		
		
		
		return mapping.findForward("photoSend");
	}
	
	public ActionForward photoReceive(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 10;
		//System.out.println("1111");
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = Integer.valueOf(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getIReceiveCommentByType(uid, PHOTO, start, limit);
		
		int count = this.commentService.getCountIReceiveCommentByType(uid, PHOTO);
		
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		
		
		//System.out.println(url);
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		
		
		
		return mapping.findForward("photoReceive");
	}
	
	public ActionForward activitySend(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 10;
		//System.out.println("1111");
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = Integer.valueOf(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getISendCommentByType(uid, ACTIVITY, start, limit);
		
		int count = this.commentService.getCountISendCommentByType(uid, ACTIVITY);
		
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		
		
		//System.out.println(url);
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		
		
		
		return mapping.findForward("activitySend");
	}
	
	public ActionForward activityReceive(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		int limit = 10;
		//System.out.println("1111");
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = Integer.valueOf(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getIReceiveCommentByType(uid, ACTIVITY, start, limit);
		
		int count = this.commentService.getCountIReceiveCommentByType(uid, ACTIVITY);
		
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		
		
		//System.out.println(url);
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		
		
		
		return mapping.findForward("activityReceive");
	}
	
	
	public Comment comment(Comment comment){
		WebContext ctx = WebContextFactory.get();
		HttpServletRequest request = ctx.getHttpServletRequest();
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		String userName = user.getUsername();
		String photo = user.getPhotoURL();
		String content = HtmlUtil.htmlEncode(comment.getContent());
		
		comment.setContent(content);
		comment.setReaded(0);
		comment.setSendPhoto(photo);
		comment.setSendUserName(userName);
		comment.setSendUserNo(uid);
		comment.setTime(new Date());
		
		this.commentService.addComment(comment);
		return comment;
	}
	
	public CommentReply commentReply(CommentReply commentReply) {
		WebContext ctx = WebContextFactory.get();
		HttpServletRequest request = ctx.getHttpServletRequest();
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		String userName = user.getUsername();
		
		String content = HtmlUtil.htmlEncode(commentReply.getContent());
		
		commentReply.setContent(content);
		commentReply.setReaded(0);
		commentReply.setReplyUserName(userName);
		commentReply.setReplyUserNo(uid);
		commentReply.setTime(new Date());
		this.commentService.addCommentReply(commentReply);
		return commentReply;
	}
	
	public int deleteSendComment(Integer commentId) {
		WebContext ctx = WebContextFactory.get();
		HttpServletRequest request = ctx.getHttpServletRequest();
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		
		this.commentService.deleteByIdAndSendUserNo(commentId, uid);
		
		return commentId;
	}
	
	
	public int deleteReceiveComment(Integer commentId) {
		WebContext ctx = WebContextFactory.get();
		HttpServletRequest request = ctx.getHttpServletRequest();
		User user = (User)request.getSession().getAttribute("user");
		int uid = user.getUserNo();
		
		
		this.commentService.deleteByIdAndReceiveUserNo(commentId, uid);
		return commentId;
	}
	
	
	public Object[] getComment(Integer foreignId, Integer type, Integer cur) {
		
		int limit = 10;
		int start = cur * limit;
		
		List<Comment> list = this.commentService.getCommentByType(foreignId, type, start, limit);
		
		int count = this.commentService.getCountCommentByType(foreignId, type);
		
		return new Object[]{list, count};
	}
	
}
